package cn.bdqn.layui.service.role;

import cn.bdqn.layui.entity.vo.MenuRoleVO;
import cn.bdqn.layui.entity.Role;
import cn.bdqn.layui.mapper.MenuMapper;
import cn.bdqn.layui.mapper.RoleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;


    @Resource
    MenuMapper menuMapper;

    @Override
    public List<Role> findRoleAll() {
        return roleMapper.findRoleAll();
    }

    @Override
    public List<Role> findRolePage(Integer id, Integer pageNo, Integer pageSize) {
        return roleMapper.findRolePage(id, pageNo, pageSize);
    }

    @Override
    public List<Role> findByIdROle(Integer uid) {
        return roleMapper.findByIdROle(uid);
    }

    @Override
    public int addRoleMenu(MenuRoleVO menuRoleVO) {
        Role role = roleMapper.findByName(menuRoleVO.getNameZH(), 0);
        int i = 0;
        if (role != null && menuRoleVO.getMenus().length > 0) {
            for (String mid : menuRoleVO.getMenus()) {
                i = menuMapper.addMenuRole(role.getId(), Integer.parseInt(mid));
            }
        }
        if (role == null && menuRoleVO.getMenus().length == 0) {
            int num = roleMapper.addRole(menuRoleVO.getName(), menuRoleVO.getNameZH());
            Role role1 = roleMapper.findByName(menuRoleVO.getNameZH(), 0);
        }
        if (role == null && menuRoleVO.getMenus().length > 0) {
            i = roleMapper.addRole(menuRoleVO.getName(), menuRoleVO.getNameZH());
            Role role1 = roleMapper.findByName(menuRoleVO.getNameZH(), 0);
            for (String mid : menuRoleVO.getMenus()) {
                menuMapper.addMenuRole(role1.getId(), Integer.parseInt(mid));
            }
        }
        return i;
    }

    @Override
    public Role findByName(String name, Integer id) {
        return roleMapper.findByName(name, id);
    }

    @Override
    public int updateRoleMenu(MenuRoleVO menuRoleVO) {
        Role role = roleMapper.findByName(menuRoleVO.getNameZH(), 0);
        int i = 0;
        List<MenuRoleVO> menuRoleRid = menuMapper.findMenuRoleRid(menuRoleVO.getRid(),0);
        if (role != null && menuRoleVO.getMenus().length >= 0) {
            for (MenuRoleVO roleVO : menuRoleRid) {
                menuMapper.delMeunRole(roleVO.getRid(),0);
            }
            for (String mid : menuRoleVO.getMenus()) {
                i = menuMapper.addMenuRole(menuRoleVO.getRid(), Integer.parseInt(mid));
            }
        }

        if (role == null && menuRoleVO.getMenus().length == 0) {
            i = roleMapper.updateRole(menuRoleVO.getName(), menuRoleVO.getNameZH(), menuRoleVO.getRid());
        }

        if (role == null && menuRoleVO.getMenus().length > 0) {
            for (MenuRoleVO roleVO : menuRoleRid) {
                menuMapper.delMeunRole(roleVO.getRid(),0);
            }
            i = roleMapper.updateRole(menuRoleVO.getName(), menuRoleVO.getNameZH(), menuRoleVO.getRid());
            for (String mid : menuRoleVO.getMenus()) {
                menuMapper.addMenuRole(menuRoleVO.getRid(), Integer.parseInt(mid));
            }
        }
        return i;
    }

    @Override
    public int delRole(Integer id) {
        return roleMapper.delRole(id);
    }

    @Override
    public List<MenuRoleVO> findRoleIdMenu(Integer rid) {
        return roleMapper.findRoleIdMenu(rid);
    }

    @Override
    public int delRoleMenu(Integer rid) {
        return roleMapper.delRoleMenu(rid);
    }
}
